package com.tool.demo.k8s;

import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.utils.Utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * <p>k8s</p>
 *
 * @author
 * @date 2022/6/8
 */
public class K8sDemo {

    private static final String KUBECONFIG_FILE = Utils.filePath(
        K8sDemo.class.getResource("/admin.conf"));

    public static void main(String[] args) {
        new K8sDemo().viewNamespace();
    }

    KubernetesClient client = getCustomKubernetesClient();

    public void createNamespace() {
        Namespace namespace = new NamespaceBuilder()
            .withNewMetadata()
            .withName("pkslow")
            .addToLabels("reason", "pkslow-sample")
            .endMetadata()
            .build();
        client.namespaces().createOrReplace(namespace);
    }

    public void viewNamespace() {
        // 查看命名空间
        NamespaceList namespaceList = client.namespaces().list();
        namespaceList.getItems()
            .forEach(namespace ->
                System.out.println(
                    namespace.getMetadata().getName() + ":" + namespace.getStatus().getPhase()));
    }

    public void createPod() {
        Pod pod = new PodBuilder()
            .withNewMetadata()
            .withName("nginx")
            .addToLabels("app", "nginx")
            .endMetadata()
            .withNewSpec()
            .addNewContainer()
            .withName("nginx")
            .withImage("nginx:1.19.5")
            .endContainer()
            .endSpec()
            .build();

        try {
            client.pods().inNamespace("pkslow").createOrReplace(pod);
        } catch (Exception e) {
            System.out.println("create pod failed");
        }

    }

    public void viewPod() {
        // 查看Pod
//        ListOptions options = new ListOptions();
//        options.setLabelSelector("app=nginx");
        Pod nginx = client.pods().inNamespace("pkslow")
            .list()
            .getItems()
            .get(0);
        System.out.println(nginx);
    }

    /*
    public void updateNamespace(){
        // 修改命名空间
        client.namespaces().withName("pkslow")
            .edit(n -> new NamespaceBuilder(n)
                .editMetadata()
                .addToLabels("project", "pkslow")
                .endMetadata()
                .build()
            );
    }

    public void updatePod(){
        // 修改Pod
        client.pods().inNamespace("pkslow").withName("nginx")
            .edit(p -> new PodBuilder(p)
                .editMetadata()
                .addToLabels("app-version", "1.0.1")
                .endMetadata()
                .build()
            );
    }
*/
    public void deleteNamespace() {
        client.pods().inNamespace("pkslow")
            .withName("nginx")
            .delete();

    }


    private static KubernetesClient getDefaultKubernetesClient() {
        //This would pick up default settings, reading your `kubeconfig` file from `~/.kube/config`
        return new DefaultKubernetesClient();
    }

    private static KubernetesClient getCustomKubernetesClient() {
//        Config config = new ConfigBuilder()
//            .withMasterUrl("https://xxx:6443")
//            .build();
        Config config = null;
        try {
            File configFile = new File(KUBECONFIG_FILE);
            final String adminConfData = String.join("\n", Files.readAllLines(configFile.toPath()));

            config = Config.fromKubeconfig(adminConfData);
        } catch (IOException e) {
            System.out.println("error");
            e.printStackTrace();
        }
        return new DefaultKubernetesClient(config);
    }


}
